var action =".htm";
jQuery(function(){
	$.divselect("#divselect_pt_status", "#status");
	$.divselect("#divselect_pt_checkStatus", "#checkStatus");
	$("#startDate").val(getFirstDateOfCurrMonth());
	$("#endDate").val(getCurrDate());
});

var query = function(){
	asyncSubmit("produceManagerForm");
};

var updateCheckStatus = function(id){
	if(window.confirm("确定要执行生产任务审核操作？")){
		var fun = function(msg){
			if (msg.result == "success") {
				layer.msg("审核成功！",1,9,function(){
					query();
				});
			} else {
				layer.alert(msg.result);
			};
		};
		
		ajaxCommFunWithDataType(globePath+"/produce/updateCheckStatus"+action, "produceId="+id,'json',fun);
	}
};

/*========================================cpt操作==============================================*/
var toUpdateCPTCheck = function(id){
	var fun =function(data){
		$("#cpt-temp-div").html(data);
		//添加明细弹窗
		$.box('.onlyuserforaddtaskalertbox', {
			onOpen : function() {
				$("#proId_cpt").val(id);
				$('#scrollbar').tinyscrollbar();
				$('#scrollbar_002').tinyscrollbar();//里面有2个滚动条*/		
			}
		}, {
			submit:{
				close:false,
				dom:['#submitCPTCheck'],
				fun:function(index){
					var currObj = this;
					updateCPTCheck(function(){					
						currObj.close(index);
					});
					
				}
			},
			close : {
				dom : [ '.onlyuserforaddtaskalertbox .closeforaddtask','.onlyuserforaddtaskalertbox .c_but_no' ]
			}
		});
	};
	
	ajaxCommFunWithDataType(globePath+"/produce/loadCPTItems"+action, 'proId='+id,'text',fun);
};

var seletCPTProcessOfLI = function(obj){
	var cpt = $(obj).find("input");
	if(cpt.val()!=1){		
		$(obj).addClass("active");
		$(obj).addClass("ready");
		cpt.val(1);
	}else{
		$(obj).removeClass("active");
		$(obj).removeClass("ready");
		cpt.val(0);
	}
};

var updateCPTCheck = function(callback){
	var fun = function(data){
		if(data.result=='success'){
			if(callback){				
				callback();
			}
			layer.msg("保存CPT流程成功！",1,9,function(){
				query();
			});
		}else{
			layer.msg(data.msg);
		}
		
	};
	ajaxCommFunWithDataType(globePath+"/produce/savePCPTProcess"+action, $("#updateCPTCheckForm").serialize(),'json',fun);
};


/*=======================================oct操作==================================================*/
var toUpdateOCTCheck = function(id){
	var fun =function(data){
		$("#oct-temp-div").html(data);
		//添加明细弹窗
		$.box('.onlyuserforaddOCTalertbox', {
			onOpen : function() {
				$("#proId_oct").val(id);
				$('#scrollbar').tinyscrollbar();
				$('#scrollbar_002').tinyscrollbar();//里面有2个滚动条*/		
			}
		}, {
			submit:{
				close:false,
				dom:['#submitOCTCheck'],
				fun:function(index){
					var currObj = this;
					updateOCTCheck(function(){					
						currObj.close(index);
					});
					
				}
			},
			close : {
				dom : [ '.onlyuserforaddOCTalertbox .closeforaddOCT','.onlyuserforaddOCTalertbox .c_but_no' ]
			}
		});
	};
	
	ajaxCommFunWithDataType(globePath+"/produce/loadOCTItems"+action, 'proId='+id,'text',fun);
};

var seletOCTProcessOfLI = function(obj){
	var oct = $(obj).find("input");
	if(oct.val()!=1){		
		$(obj).addClass("active");
		$(obj).addClass("ready");
		oct.val(1);
	}else{
		$(obj).removeClass("active");
		$(obj).removeClass("ready");
		oct.val(0);
	}
};

var updateOCTCheck = function(callback){
	var fun = function(data){
		if(data.result=='success'){
			if(callback){				
				callback();
			}
			layer.msg("保存OCT流程成功！",1,9,function(){
				query();
			});
		}else{
			layer.msg(data.msg);
		}
		
	};
	ajaxCommFunWithDataType(globePath+"/produce/savePOCTProcess"+action, $("#updateOCTCheckForm").serialize(),'json',fun);
};


var updateStatus = function(id,status){
	var str = "";
	if(status==2){
		str = "完成";
	}
	if(status==3){
		str = "验收";
	}
	layer.confirm("确定执行【"+str+"】操作？",function(){
		var fun = function(msg) {
			if (msg.result == "success") {
				layer.msg("执行【"+str+"】操作成功！",1,9,function(){
					query();
				});
			} else {
				layer.alert(msg.result);
			};
		};
		
		ajaxCommFunWithDataType(globePath+"/produce/updateStatus"+action, "produceId="+id+"&status="+status,'json',fun);
	});
};

//删除明细
var deleteProduce  = function(id){
	if(window.confirm("确定要删除该生产单？")){
		var fun = function(msg) {
			if (msg.result == "success") {
				layer.msg("删除生产单成功！",1,9,function(){
					query();
				});
			} else {
				layer.alert(msg.result);
			};
		};
		
		ajaxCommFunWithDataType(globePath+"/produce/deleteProduce"+action, "produceId="+id,'json',fun);
	}
};